<template>
  <div class="step-container">
    <div class="steps">
      <div class="step-item" v-for="item in sequenceArr" :key="item">
        <img :src="imgSource.loading" alt="" v-if="stepOptions[item].length === 1" class="rotate" />
        <img :src="imgSource.complete" alt="" v-else />
        <div class="title">{{ getTitle(item) }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import { groupBy } from '../utils/tools';
export default {
  props: {
    // 步骤内容
    steps: {
      type: Array,
      default: () => [],
      required: true
    }
  },
  data() {
    return {
      imgSource: {
        loading:
          '//i.thsi.cn/staticS3/mobileweb-upload-static-server.img/m/atom/6d04ea76-a21f-4553-b655-b9abe605c316.png',
        complete:
          '//i.thsi.cn/staticS3/mobileweb-upload-static-server.img/m/atom/57558743-56a4-40a0-81c0-a53d5b569f42.png'
      },
      stepOptions: {},
      //  顺序数组，用以保存后端返回的顺序，并以其为准
      sequenceArr: []
    };
  },
  methods: {
    getTitle(item) {
      return this.stepOptions?.[item]?.[0]?.body?.node_name || '';
    }
  },
  watch: {
    steps: {
      handler(val) {
        const sequenceArr = [];
        const filteredSteps = val.filter(item => {
          if (!['CHART', 'EXPLAIN'].includes(item.type)) {
            if (item.body.status === 'START') {
              sequenceArr.push(item.body.node_id);
            }
            return true;
          }
        });
        this.sequenceArr = sequenceArr;
        this.stepOptions = groupBy(filteredSteps, item => item.body.node_id);
      },
      deep: true,
      immediate: true
    }
  }
};
</script>
<style lang="less" scoped>
@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
.step-container {
  border-left: 1px dashed #4f4f5e;
  padding: 8px 0 8px 16px;
  margin-left: 8px;
  .steps {
    display: flex;
    flex-direction: column;
    gap: 16px;
    .step-item {
      display: flex;
      img {
        width: 16px;
        height: 16px;
        margin-right: 8px;
        &.rotate {
          animation: rotate 1s linear infinite;
        }
      }
      .title {
        font-family: PingFangSC-Regular;
        font-size: 13px;
        color: #d1d1e0;
        letter-spacing: 0;
        line-height: 20px;
        font-weight: 400;
      }
    }
  }
}
</style>
